package com.soft.cake.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.soft.cake.mapper.ImgMapper;
import com.soft.cake.pojo.Img;
import com.soft.cake.pojo.Product;
import com.soft.cake.service.IImgService;
@Service
public class ImgServiceImpl implements IImgService {
	@Autowired
	private ImgMapper imgmapper;

	/**
	 * @author Huo Sa
	 * @description 根据产品id找到图片
	 * @param productId
	 * @return Img
	 */
	@Override
	public Img selectByProductKey(Integer productId) {
		List<Img> imgs = imgmapper.selectByProductId(productId);
		if(imgs == null) {
			imgs = new ArrayList<Img>();
		}
		if (imgs.size() <= 0) {
			Img img = new Img();
			img.setImgName("None");
			img.setProductId(productId);
			img.setImgUrl("/front/images/m2.png");
			imgs.add(img);
		}
		return imgs.get(0);
	}
	
	/**
	 * @author Huo Sa
	 * @description 获取每个商品库中第一张图片
	 * @param List<Product>
	 * @return List<Img>
	 */
	public List<Img> listImgOfProducts(List<Product> products) {
		List<Img> imgs = new ArrayList<Img>();
		for(Product product : products) {
			List<Img> items = imgmapper.selectByProductId(product.getProductId());
			if(items == null) {
				items = new ArrayList<Img>();
			}
			if (items.size() <= 0) {
				Img img = new Img();
				img.setImgName("None");
				img.setProductId(product.getProductId());
				img.setImgUrl("/front/images/m2.png");
				items.add(img);
			}
			imgs.add(items.get(0));
		}
		return imgs;
	}

	/**
	 * @author Yanyan Shi
	 * @description 通过商品id获得商品图片list
	 * @param productId
	 * @param 
	 */
	@Override
	public List<Img> imgList(int productId) {
		// TODO Auto-generated method stub
		List<Img> imgList = imgmapper.selectByProductId(productId); 
		if (imgList==null) {
			return null;
		}
		return imgList;
	}
}
